package app.auto.runner.base.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import app.auto.runner.base.SPrefUtil;

public class MoveView extends LinearLayout {


    private float currentX = -1;

    private float currentY = -1;

    private Paint paint = new Paint();
    private Context context;


    public MoveView(Context context) {

        super(context);

        // TODO Auto-generated constructor stub

    }


    public MoveView(Context context, AttributeSet set) {

        // TODO Auto-generated constructor stub
        super(context, set);
        this.context = context;


    }

    /*
     *设置控件所在的位置YY，并且不改变宽高，
     * XY为绝对位置
     */
    public static void setLayout(View view, Float x, Float y) {
        view.setX(x);
        view.setY(y);
//        MarginLayoutParams margin =new MarginLayoutParams(view.getLayoutParams());
//        margin.setMargins(x.intValue(), y.intValue(), x.intValue() + margin.width, y.intValue() + margin.height);
//        RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(margin);
//        view.setLayoutParams(layoutParams);

    }


    @Override

    protected void onDraw(Canvas canvas) {

        // TODO Auto-generated method stub
        if (currentX != -1) {
            setLayout(this, currentX, currentY);
        } else {
            currentX = ((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getWidth() * 75 / 100;
            currentY = ((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getHeight() * 50 / 100;
        }
        super.onDraw(canvas);


    }

    public void setContext(Context context) {
        this.context = context;
        SPrefUtil.iniContext(((Activity) context).getApplication());
        if(((String)SPrefUtil.getValue("xy", "x", String.class)).equals("")){
            currentX = ((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getWidth() * 75 / 100;
            currentY = ((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getHeight() * 50 / 100;

        }else {
            currentX = Float.parseFloat((String) SPrefUtil.getValue("xy", "x", String.class));
            currentY = Float.parseFloat((String) SPrefUtil.getValue("xy", "y", String.class));
        }

    }

//当view发生触屏事件时会调用此方法

    float balanceX;
    float balanceY;

    @Override

    public boolean onTouchEvent(MotionEvent event) {

        // TODO Auto-generated method stub
        if (event.getAction() != MotionEvent.ACTION_UP) {

            if (balanceX == 0) {
                balanceX = getX() - event.getRawX();
                balanceY = getY() - event.getRawY();

            }

            currentX = event.getRawX() + balanceX;

            currentY = event.getRawY() + balanceY;
            SPrefUtil.putValue(context, "xy", "x", currentX + "");
            SPrefUtil.putValue(context, "xy", "y", currentY + "");

        } else {
            balanceX = 0;
            balanceY = 0;
        }
        //通知当前组件重绘自己

        invalidate();

        return true;

    }


}
